package jsimus;

import java.io.File;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class Zones {

	XMLReader xml = new XMLReader();

	final private String ZONES_CATALOG_PATH = "zones_list\\";
	String room_name;
	String room_description;
	String exits[];

	private int current_zone_number = 1;
	private int current_room_number = 1;

	public String getRoomName() {
		return room_name;
	}

	public void setRoomName(String room_name) {
		this.room_name = room_name;
	}

	public String getRoomDescription() {
		return room_description;
	}

	public void setRoomDescription(String room_description) {
		this.room_description = room_description;
	}

	public int getCurrentZoneNumber() {
		return this.current_zone_number;
	}

	public void setCurrentZoneNumber(int current_zone_number) {
		this.current_zone_number = current_zone_number;
	}

	public int getCurrentRoomNumber() {
		return this.current_room_number;
	}

	public void setCurrentRoomNumber(int current_room_number) {
		this.current_room_number = current_room_number;
	}

	public String[] getExits() {
		return exits;
	}

	public void setExits(String exits) {
		this.exits = exits.split(" ");
	}

	public String readZone() {
		String path = null;
		String node = null;
		try {
			File folder = new File(this.ZONES_CATALOG_PATH);

			for (File f : folder.listFiles()) {
				String parsed_zone_number = f.getName().replace(".xml", "");

				int file_zone_number = Integer.parseInt(parsed_zone_number);
				if (getCurrentZoneNumber() == file_zone_number)
					path = f.getAbsolutePath();
			}

			File zone_file = new File(path);

			DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
			DocumentBuilder db = dbf.newDocumentBuilder();
			Document doc = db.parse(zone_file);
			doc.getDocumentElement().normalize();

			NodeList nodeLst = doc.getElementsByTagName("room");

			for (int s = 0; s < nodeLst.getLength(); s++) {

				Node parseNode = nodeLst.item(s);

				if (parseNode.getNodeType() == Node.ELEMENT_NODE) {
					Element parseElement = (Element) parseNode;
					NodeList roomIdElementList = parseElement
							.getElementsByTagName("id");
					Element roomIdNmElmnt = (Element) roomIdElementList.item(0);
					NodeList roomIdNodeChilds = roomIdNmElmnt.getChildNodes();
					if (getCurrentRoomNumber() == Integer
							.parseInt((((Node) roomIdNodeChilds.item(0))
									.getNodeValue()))) {

						NodeList nameElementList = parseElement
								.getElementsByTagName("name");
						Element nameNmElmnt = (Element) nameElementList.item(0);
						NodeList nameNodeChilds = nameNmElmnt.getChildNodes();
						setRoomName(((Node) nameNodeChilds.item(0))
								.getNodeValue());

						NodeList descriptionElementList = parseElement
								.getElementsByTagName("description");
						Element descriptionNmElmnt = (Element) descriptionElementList
								.item(0);
						NodeList descriptionNodeChilds = descriptionNmElmnt
								.getChildNodes();
						setRoomDescription(((Node) descriptionNodeChilds
								.item(0)).getNodeValue());

						NodeList exitsElementList = parseElement
								.getElementsByTagName("exits");
						Element exitsNmElmnt = (Element) exitsElementList
								.item(0);
						NodeList exitsNodeChilds = exitsNmElmnt.getChildNodes();
						setExits(((Node) exitsNodeChilds.item(0))
								.getNodeValue());
					}
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return node;
	}
}
